package com.pan.admin.module.distribute.service.impl;

import com.pan.admin.common.utils.DateUtils;
import com.pan.admin.common.utils.SecurityUtils;
import com.pan.admin.common.utils.uuid.IdUtils;
import com.pan.admin.module.distribute.dao.LoanDistributeGroupDao;
import com.pan.admin.module.distribute.entity.LoanDistributeGroup;
import com.pan.admin.module.distribute.service.LoanDistributeGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 智能分配组Service业务层处理
 *
 * @author ts
 * @date 2023-09-10
 */
@Service
public class LoanDistributeGroupServiceImpl implements LoanDistributeGroupService {
    @Autowired
    private LoanDistributeGroupDao loanDistributeGroupDao;

    /**
     * 查询智能分配组
     *
     * @param id 智能分配组主键
     * @return 智能分配组
     */
    @Override
    public LoanDistributeGroup selectLoanDistributeGroupById(Long id) {
        return loanDistributeGroupDao.selectLoanDistributeGroupById(id);
    }

    /**
     * 查询智能分配组列表
     *
     * @param loanDistributeGroup 智能分配组
     * @return 智能分配组
     */
    @Override
    public List<LoanDistributeGroup> selectLoanDistributeGroupList(LoanDistributeGroup loanDistributeGroup) {
        return loanDistributeGroupDao.selectLoanDistributeGroupList(loanDistributeGroup);
    }

    /**
     * 新增智能分配组
     *
     * @param loanDistributeGroup 智能分配组
     * @return 结果
     */
    @Override
    public int insertLoanDistributeGroup(LoanDistributeGroup loanDistributeGroup) {
        String id = IdUtils.fastSimpleUUID();
        String sourceLinkProxyUrl = loanDistributeGroup.getLinkProxyUrl();

        loanDistributeGroup.setUniqueCode(id);
        loanDistributeGroup.setCreateTime(DateUtils.getNowDate());
        //  修复URL
        String uniqueLink = sourceLinkProxyUrl.endsWith("/") ? sourceLinkProxyUrl : sourceLinkProxyUrl + "/";
        loanDistributeGroup.setLinkProxyUrl(uniqueLink + id);
        loanDistributeGroup.setCreateBy(SecurityUtils.getUsername());
        return loanDistributeGroupDao.insertLoanDistributeGroup(loanDistributeGroup);
    }

    /**
     * 修改智能分配组
     *
     * @param loanDistributeGroup 智能分配组
     * @return 结果
     */
    @Override
    public int updateLoanDistributeGroup(LoanDistributeGroup loanDistributeGroup) {
        loanDistributeGroup.setUpdateTime(DateUtils.getNowDate());
        loanDistributeGroup.setUpdateBy(SecurityUtils.getUsername());
        return loanDistributeGroupDao.updateLoanDistributeGroup(loanDistributeGroup);
    }

    /**
     * 批量删除智能分配组
     *
     * @param ids 需要删除的智能分配组主键
     * @return 结果
     */
    @Override
    public int deleteLoanDistributeGroupByIds(Long[] ids) {
        return loanDistributeGroupDao.deleteLoanDistributeGroupByIds(ids);
    }

    /**
     * 删除智能分配组信息
     *
     * @param id 智能分配组主键
     * @return 结果
     */
    @Override
    public int deleteLoanDistributeGroupById(Long id) {
        return loanDistributeGroupDao.deleteLoanDistributeGroupById(id);
    }
}
